/**
 * 客户资源录入脚本支持
 */
var clientKey = null;
var modifydata = null;
var studentKey = null;
var saveAfterClose = false;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
	init_editors();

	client_detailed( $( "#tbl_client" ), false );

	// 初始化选择
	if( !empty( clientKey ) && !empty( modifydata ) )
	{
		clientSelectedHandler( modifydata );
	}
} );

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

var _editor = [];

function button_bind()
{
	$( "#btnSelect" ).bind( "click", function()
	{
		// 若更换客户则重置studentKey
		studentKey = null;

		parent.clienter.setCallback( clientSelectedHandler );
		parent.clienter.reset();
		parent.clienter.open();
	} );

	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	// 增加新学员
	$( "#btnAddStudent" ).click( function()
	{
		if( empty( clientKey ) )
		{
			//infopad.err( "请选择需要编辑学员资料的目标客户。" );
			return;
		}

		// 增加学员表单
		setNewStudent();

		// 添加ID到格式化对象库
		init_editors();

		// 更新背景
		update_row_bg();
	} );

	$( "#btn_detailed" ).click( function( e )
	{
		e.stopPropagation();

		var show = $( "#tbl_client" ).find( "tr[rel='hide']" ).eq( 0 ).visible();
		client_detailed( $( "#tbl_client" ), !show );
	} );

	// 若修改指定的学员，则隐藏更换客户按钮
	if( !empty( studentKey ) )
	{
		$( "#btnSelectBox" ).hide();
	}
}

function init_editors()
{
	$( "div[contenteditable='true']" ).each( function()
	{
		if( _editor.indexOf( $( this ).attr( "id" ) ) == -1 )
			_editor.push( $( this ).attr( "id" ) );
	} );
}

function client_detailed( tar, visible )
{
	if( visible )
		tar.find( "tr[rel='hide']" ).show();
	else
		tar.find( "tr[rel='hide']" ).hide();

	setTableRowBackColor( tar );
}

function select_date( targetid, func )
{
	$( '#' + targetid ).Zebra_DatePicker( {
		format: 'Y-m-d',
		show_icon: false,
		onSelect: func
	} );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function clientSelectedHandler( data )
{
	if( typeof ( data ) == "undefined" )
		return;

	// 有效用户检测
	if( data.invalid == 1 || data.source == "cru0943rdrf2" || data.category == "510a4qf1i57p" )
	{
		infopad.err( "无效、未知、广告推销类型的客户无须添加学员资料。" );
		return;
	}

	setClientData( data );

	if( clientKey != null )
	{
		console.log( "student request->" );
		request_student( clientKey );
	}
}

function setClientData( data )
{
	// 客户资料
	if( typeof ( data ) == "object" && data.nickname )
	{
		clientKey = data.key;

		$( "#nickname" ).html(
			data.nickname
			+ ( data.invalid == 1 ? "<em class='caption'>（无效客户）</em>"
				: "<em class='valid'>（有效客户）</em>" ) );
		$( "#category" ).html( shtml( data.categoryns ) );
		$( "#source" ).html( shtml( data.sourcens ) );
		$( "#keyword" ).html( shtml( data.keyword ) );
		$( "#address" ).html( data.province + " " + data.city + " " + data.address );
		$( "#phone" ).html( shtml( data.phone ) );
		$( "#email" ).html( shtml( data.email ) );
		$( "#qq" ).html( shtml( data.qq ) );
		$( "#ip" ).html( shtml( data.ip ) );
	}
	else
	{
		clientKey = null;

		$( "#nickname" ).html( "&nbsp;" );
		$( "#category" ).html( "&nbsp;" );
		$( "#source" ).html( "&nbsp;" );
		$( "#address" ).html( "&nbsp;" );
		$( "#keyword" ).html( "&nbsp;" );
		$( "#phone" ).html( "&nbsp;" );
		$( "#email" ).html( "&nbsp;" );
		$( "#qq" ).html( "&nbsp;" );
		$( "#ip" ).html( "&nbsp;" );
	}
}

function shtml( val )
{
	if( empty( val ) )
		return "<em class='caption'>未填写</em>";
	return val;
}

function request_student( key )
{
	$.post( "student_search.php", {
		key: key
	}, function( d )
	{
		if( d.err == 0 )
		{
			$( "#studentlst" ).empty();
			console.log( "student getted!" );
			var size = parseInt( d[ 'size' ] );
			for( var i = 0; i < size; i++ )
			{
				var v = d[ i ];

				if( !empty( studentKey ) )
				{
					if( v.key == studentKey )
						setNewStudent( v );
				}
				else
				{
					setNewStudent( v );
				}
			}
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function submit_form()
{
	var data = {};

	// 客户key
	data.client = clientKey;
	if( empty( data.client ) )
	{
		infopad.err( "未指定学员相关的目标客户。" );
		return;
	}

	// 待更新的学员资料
	var student = get_students();
	data.student = JSON.stringify( student );

	submitter.post( "consultant_student_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );
			location.reload();

			if( saveAfterClose )
				close_this_model();
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
	else
		if( e.ctrlKey && e.shiftKey && e.which == 70 )// ctrl+shift+f
		{
			var edt = $( document.activeElement );
			var id = edt.attr( "id" );

			if( _editor.indexOf( id ) > -1 )
			{
				edt.html( htmlfilter( edt.html() ) );
			}
		}
		else
			if( e.ctrlKey && e.shiftKey && e.which == 65 )// ctrl+shift+a
			{
				setNewStudent();
			}
}
